package com.example.shiro.service.impl;

import com.example.shiro.po.Permission;
import com.example.shiro.po.Role;
import com.example.shiro.po.User;
import com.example.shiro.service.LoginService;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.stereotype.Service;

/**
 * @author xyh
 * @description
 * @date 2021/4/1 16:12
 */
@Service
public class LoginServiceImpl implements LoginService {


    @Override
    public User login(String userName, String password) {
        if(!userName.equals("111") || !password.equals("111")){
            throw new RuntimeException("账号或密码错误");
        }


//        User user = new User();
//        user.setId(1);
//        user.setUserName(userName);
//        user.setPwasword(password);
//
//        Role role = new Role();
//        role.setId(1);
//        role.setName("管理员");
//        Permission permission = new Permission();
//        permission.setId(1);
//        permission.setName("用户管理");
//        role.getPermission().add(permission);
//
//        Role role1 = new Role();
//        role1.setId(1);
//        role1.setName("测试人员");
//        Permission permission1 = new Permission();
//        permission1.setId(1);
//        permission1.setName("登陆");
//        role1.getPermission().add(permission1);
//
//        user.getRoles().add(role);
//        user.getRoles().add(role1);
        return null;
    }

    @Override
    public User getUserByUserName(String userName) {

        User user = new User();
        user.setId(1);
        user.setName("小李");
        user.setUserName(userName);
        user.setSalt("111");
        SimpleHash hash = new SimpleHash("MD5", "111", user.getSalt());
        user.setPwasword(hash.toHex());

        Role role = new Role();
        role.setId(1);
        role.setName("管理员");
        Permission permission = new Permission();
        permission.setId(1);
        permission.setName("用户管理");
        role.getPermission().add(permission);

        Role role1 = new Role();
        role1.setId(1);
        role1.setName("测试人员");
        Permission permission1 = new Permission();
        permission1.setId(1);
        permission1.setName("list");
        role1.getPermission().add(permission1);

        Permission permission2 = new Permission();
        permission2.setId(2);
        permission2.setName("add");
        role1.getPermission().add(permission2);

        user.getRoles().add(role);
        user.getRoles().add(role1);
        return user;
    }
}
